package com.qingke.oms.pojo;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="shopping_cart")
public class ShoppingCart {
	@Id
	@Column(name="id")
	private int id;
	
	@ManyToOne
	@JoinColumn(name="customer_id")
	private User customer;
	
	@ManyToOne
	@JoinColumn(name="shop_id")
	private Shop shop;
	
	@OneToMany(mappedBy="shoppingCart",fetch=FetchType.LAZY)
	private Set<ShoppingCartItem> shoppingCartItems;
	
	@Column(name="total_price")
	private double total;
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public User getCustomer() {
		return customer;
	}

	public void setCustomer(User customer) {
		this.customer = customer;
	}

	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	public Set<ShoppingCartItem> getShoppingCartItems() {
		return shoppingCartItems;
	}

	public void setShoppingCartItems(Set<ShoppingCartItem> shoppingCartItems) {
		this.shoppingCartItems = shoppingCartItems;
	}

	public double getTotal() {
		return total;
	}

	public void setTotal(double total) {
		this.total = total;
	}

	public void calculateTotal(){
		double count=0;
		for(ShoppingCartItem i:shoppingCartItems){
			if(i.getQuantity()!=0){
				count+=(i.getPrice()*i.getQuantity());
			}
		}
		setTotal(count);
	}
	
}
